package com.warrant.module.rpa.finance.util;

import org.apache.commons.jexl3.JexlBuilder;
import org.apache.commons.jexl3.JexlContext;
import org.apache.commons.jexl3.JexlEngine;
import org.apache.commons.jexl3.JexlScript;

/**
 * jexl引擎
 * @author cn
 *
 */
public class JexlUtil {

    private static JexlEngine engine;

    private JexlUtil() {
    }

    private static JexlEngine getJexlEngine() {
        if (engine == null) {
            synchronized (JexlUtil.class) {
                if (engine == null) {
                    engine = new JexlBuilder().create();
                }
            }
        }
        return engine;
    }

    public static Object execute(String expression, JexlContext context) {
        JexlEngine jexlEngine = getJexlEngine();
        JexlScript script = jexlEngine.createScript(expression);
        return script.execute(context);
    }
}
